export default {
  namespaced: true,
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  mutations: {
    heroDel(state, payload) {
      state.goodList.splice(payload, 1)
    },
    heroChecked(state, payload) { 
      state.goodList[payload].checked=!state.goodList[payload].checked
    },
    heroNum(state, { index, val }) {
      if(state.goodList[index].num+val===0) return
      state.goodList[index].num += val
      
    },
    isALL(state, payload) { 
      console.log("调用了",payload);
      state.goodList.forEach(item => {
        console.log(item);
        item.checked = payload
      })
    }
  },
  actions: {},
  getters: {
    sum (state) {
      return state.goodList.filter(item => item.checked === true).reduce((sum, item) => {
        return sum + item.num
      }, 0)
    },
    sumprice (state) {
      return state.goodList.filter(item => item.checked === true).reduce((sum, item) => {
        return sum + item.num * item.price
      }, 0)
    }
  },
}
